require 'rexml/document'
require 'Twitter/TwitClient.rb'
require 'Flickr/FlickClient.rb'

class Canvas < FXMainWindow
  def initialize(xaApp,sTitle)
    super(xaApp, sTitle,:opts => DECOR_ALL, :width => 800, :height => 600)
    
    @canvas = FXCanvas.new(self, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT)
    @canvas.connect(SEL_PAINT,method(:onCanvasRepaint))
	
	@xaParent = xaApp

	@tcTwitter = TwitClient.new(@@sAuthString)
	@fcFlickr = FlickClient.new()
	
	@aTwitFeeds = Array.new
	@sTweetText = 'Please Wait'
	@aTags = Array.new
	
	@igPic = nil
  end
  
  def create
    super
    show(PLACEMENT_SCREEN)
	self.maximize
	@xaParent.addTimeout(3000,:repeat=>true){ ChangeDisplay() }
  end
  
  def onCanvasRepaint(sender, sel, event)
	  FXDCWindow.new(@canvas, event){|dc|
		  dc.foreground = 0x000000
		  dc.fillRectangle(event.rect.x, event.rect.y, event.rect.w, event.rect.h)

		  dfFont = FXFont.new(getApp(), "Courier", 15, FONTWEIGHT_NORMAL)
		  dfFont.create
		  dc.font = dfFont
		  dc.foreground = 0xFFFFFF

		  dc.drawText((self.width/2)-(dfFont.getTextWidth(@sTweeter)/2),5+dfFont.getTextHeight(@sTweeter),@sTweeter)
		  dc.drawText((self.width/2)-(dfFont.getTextWidth(@sTweetText)/2),10+dfFont.getTextHeight(@sTweetText)*2,@sTweetText)
		  dc.drawText((self.width/2)-(dfFont.getTextWidth(@sThisTag)/2),(self.height-5)-dfFont.getTextHeight(@sThisTag),@sThisTag)
		  
		  if(@igPic != nil)
			dc.drawImage(@igPic,(self.width/2)-(@igPic.width/2),(self.height/2)-(@igPic.height/2))
		  end
		  dc.end
	  }
  end
		  
  def ChangeDisplay()
	#begin
		hTweetDetails = nil
		aImages = Array.new
		begin
			if(@aTags.length == 0)
				hTweetDetails = GetRandomTwitterFeed()
				@aTags = GetUniqueTagValues(hTweetDetails['tweet'])
			end
		
			iRnd = rand(@aTags.length)
			@sThisTag = @aTags[iRnd]
			@aTags.delete_at(iRnd)

			aImages = @fcFlickr.GetImagesByTag(@sThisTag);
		end	 while(aImages == nil || aImages.length == 0)
		iTempImg = nil
		begin
			iRandImg = rand(aImages.length)
			puts aImages.length
			puts aImages[iRandImg]
			iTempImg = @fcFlickr.ExtractImage(@xaParent,aImages[iRandImg])
			puts iTempImg.class
		end while (iTempImg == nil)
										  
		#Wait until after we have an image to set the text, otherwise if the
		#form calls refresh on itself, the image and text will be wrong
		if(hTweetDetails != nil)
			@sTweetText = hTweetDetails['tweet']
			@sTweeter = hTweetDetails['name']
		end
		if(iTempImg != nil)
			@igPic = iTempImg
		end
	#end while (iTempImg == null);
	#Flickr dont give us images in 800x600, so we get a larger one and scale it down
	#mImag = null;
	#mImag = iTempImg.GetThumbnailImage(800, 600, null, IntPtr.Zero);
	@canvas.update
  end

  def GetRandomTwitterFeed()
	#Gets a list of top feeds then extracts and removes one at random.
	#When the list is empty, contact Twitter for a new list
	if(@aTwitFeeds==nil||@aTwitFeeds.length==0)
		@aTwitFeeds = @tcTwitter.GetFriendsTimeline(40)
	end

	if(@aTwitFeeds == nil)
		return nil
	end

	iIndex = rand(@aTwitFeeds.length)
	
	hReturn = {}
	hReturn['name'] = @aTwitFeeds[iIndex]['name']
	hReturn['tweet'] = @aTwitFeeds[iIndex]['tweet']
	
	@aTwitFeeds.delete_at(iIndex)
												  
	return hReturn
  end

  def GetUniqueTagValues(sTwitText)
	aTags = Array.new
	sTwitText.split.each{|sTag|
		#We dont want any duplicates or very small words
		#Also remove anything that is non-alphanumeric
		if(!aTags.include?(sTag) && sTag.length>4)
			aTags << sTag
		end
	}
	return aTags
  end
end